<template>
  <el-dialog :title="!dataForm.itemId ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
      label-width="80px">
      <el-form-item label="订单号" prop="orderId">
        <el-input v-model="dataForm.orderId" placeholder="所属订单号"></el-input>
      </el-form-item>
      <el-form-item label="SKUID" prop="skuId">
        <el-input v-model="dataForm.skuId" placeholder="购买SKUID"></el-input>
      </el-form-item>
      <el-form-item label="数量" prop="quantity">
        <el-input v-model="dataForm.quantity" placeholder="购买数量"></el-input>
      </el-form-item>
      <el-form-item label="成交单价" prop="price">
        <el-input v-model="dataForm.price" placeholder="成交单价"></el-input>
      </el-form-item>
      <el-form-item label="购买属性" prop="attributes">
        <el-input v-model="dataForm.attributes" placeholder="购买时属性快照"></el-input>
      </el-form-item>
      <el-form-item label="小计
" prop="totalmoney">
        <el-input v-model="dataForm.totalmoney" placeholder="小计
"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        itemId: 0,
        orderId: '',
        skuId: '',
        quantity: '',
        price: '',
        attributes: '',
        totalmoney: ''
      },
      dataRule: {
        orderId: [
          { required: true, message: '所属订单号不能为空', trigger: 'blur' }
        ],
        skuId: [
          { required: true, message: '购买SKUID不能为空', trigger: 'blur' }
        ],
        quantity: [
          { required: true, message: '购买数量不能为空', trigger: 'blur' }
        ],
        price: [
          { required: true, message: '成交单价不能为空', trigger: 'blur' }
        ],
        attributes: [
          { required: true, message: '购买时属性快照不能为空', trigger: 'blur' }
        ],
        totalmoney: [
          { required: true, message: '小计不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init(id) {
      this.dataForm.itemId = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.itemId) {
          this.$http({
            url: this.$http.adornUrl(`/order/orderitem/info/${this.dataForm.itemId}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 200) {
              this.dataForm.orderId = data.data.orderId
              this.dataForm.skuId = data.data.skuId
              this.dataForm.quantity = data.data.quantity
              this.dataForm.price = data.data.price
              this.dataForm.attributes = data.data.attributes
              this.dataForm.totalmoney = data.data.totalmoney
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/order/orderitem/${!this.dataForm.itemId ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'itemId': this.dataForm.itemId || undefined,
              'orderId': this.dataForm.orderId,
              'skuId': this.dataForm.skuId,
              'quantity': this.dataForm.quantity,
              'price': this.dataForm.price,
              'attributes': this.dataForm.attributes,
              'totalmoney': this.dataForm.totalmoney
            })
          }).then(({ data }) => {
            if (data && data.code === 200) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
